//
//  LBMStatusOriginalView.swift
//  微博
//
//  Created by 刘保满 on 16/5/24.
//  Copyright © 2016年 itheima. All rights reserved.
// 原创微博视图

import UIKit
import SnapKit

//间距
let StatusCellMargin: CGFloat = 8


class LBMStatusOriginalView: UIView {
    
    //底部约束属性
    var bottomCon: Constraint?
    
    
    var statusViewModel: LBMStatusViewModel? {
        //给子控件设置数据
        didSet {
            
            headIcon.sd_setImageWithURL(statusViewModel?.headImageURL)
            nameLabel.text = statusViewModel?.status?.user?.name
            mbrankView.image = statusViewModel?.mbImage
            verifiedImageView.image = statusViewModel?.verifiedImage
            
            timeLabel.text = statusViewModel?.time
            
            sourceLabel.text = statusViewModel?.source
  
            contentLabel.text = statusViewModel?.status?.text
            
//            pictureView.pictures = statusViewModel?.status?.pic_urls
            
            //在给图片赋值之前需要进行判断,修改这个原创微博的底部视图
            self.bottomCon?.uninstall()
            if let urls = statusViewModel?.status?.pic_urls where urls.count > 0 {
                
                //有配图
                //更新约束
                self.snp_updateConstraints(closure: { (make) in
                    
                    self.bottomCon =  make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
    
                })

                //设置数据
                pictureView.pictures = urls
                
                //显示配图图片
                pictureView.hidden = false
                
            } else {
                
                //没有配图的情况
                //更新约束
                self.snp_updateConstraints(closure: { (make) in
                    
                    self.bottomCon = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                
                })
                //设置数据
                pictureView.pictures = nil
                
                //隐藏配图视图
                pictureView.hidden = true

            }
            

            
        }
        
    }
    
 
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    //默认报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加子控件,设置约束
    private func setupUI() {
        //视图背景
        backgroundColor = UIColor.whiteColor()
        //分隔条的背景
        sepView.backgroundColor = UIColor.redColor()
        //头像设置圆角
        headIcon.layer.cornerRadius = 20
        
        addSubview(sepView)
        addSubview(headIcon)
        addSubview(verifiedImageView)
        addSubview(nameLabel)
        addSubview(mbrankView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
  
        //设置约束
        //分隔条
        sepView.snp_makeConstraints { (make) in
            
            make.left.top.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
    
        }
        
        //头像
        headIcon.snp_makeConstraints { (make) in
            
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.width.equalTo(40)
            make.height.equalTo(40)
            
        }
        
        //认证
        verifiedImageView.snp_makeConstraints { (make) in
            
            make.right.bottom.equalTo(headIcon)
            
        }
        
        //名称
        nameLabel.snp_makeConstraints { (make) in
            
            make.left.equalTo(headIcon.snp_right).offset(StatusCellMargin)
            make.top.equalTo(headIcon.snp_top)
   
        }
        
        //等级
        mbrankView.snp_makeConstraints { (make) in
            
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
            make.top.equalTo(headIcon.snp_top)
            
        }
        //发送时间
        timeLabel.snp_makeConstraints { (make) in
            
            make.left.equalTo(headIcon.snp_right).offset(StatusCellMargin)
            
            make.bottom.equalTo(headIcon.snp_bottom)
        }
        
        //来源
        sourceLabel.snp_makeConstraints { (make) in
            
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
            
            make.bottom.equalTo(headIcon.snp_bottom)
        }
        //正文
        contentLabel.snp_makeConstraints { (make) in
            
            make.top.equalTo(headIcon.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(headIcon.snp_left)
            
        }
        
        //配图视图
        pictureView.snp_makeConstraints { (make) in
            
            
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentLabel.snp_left)
            
            //用来测试
            make.size.equalTo(CGSize(width: 100, height: 100))
            
            
        }
        
        
    
        //给这个view本身添加约束
        self.snp_makeConstraints { (make) in
            //记录当前的约束
        self.bottomCon = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        
        }
        
    }

    //懒加载子控件
    //分隔条
    private lazy var sepView: UIView = UIView()

    //头像
    private lazy var headIcon: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    //认证头像
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //名称
    private lazy var nameLabel: UILabel = UILabel(title: "张三", font: 15, textColor: UIColor.darkGrayColor())
    //等级
    private lazy var mbrankView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    //发送时间
    private lazy var timeLabel: UILabel = UILabel(title: "14:52", font: 12, textColor: UIColor.orangeColor())
    
    //来源
    private lazy var sourceLabel: UILabel = UILabel(title: "iPhone", font: 12, textColor: UIColor.darkGrayColor())
    
    //正文
        private lazy var contentLabel: UILabel = UILabel(title: "彩虹形成的全过程彩虹形成的全过程彩虹形成的全过程彩虹形成的全过程彩虹形成的全过程彩虹形成的全过程彩虹形成的全过程彩虹形成的全过程彩虹形成的全过程彩虹形成的全过程彩虹形成的全过程", font: 15, textColor: UIColor.darkGrayColor(), alignmet: .Left)

    //配图视图
    private lazy var pictureView: LBMStatusPictureView = LBMStatusPictureView()
    
    
    
    
    
    
    
}
